package com.fisicaapp;

 

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ActEnergiaMec extends Activity {

	private Button estado;
	private Button datos;
	int bestado = 0; // 0 -> iniciar 1 -> pausar 3->Reanudar
	private  MecView mecview ;
	private TextView textEp;
	private TextView textEc;
	private TextView textEm;
	private TextView textalt;
	private TextView textmasa;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.appmecanica);
	    RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout_animacion);
	    mecview = new MecView(this);
	    layout.addView(mecview);

	    estado =(Button) findViewById(R.id.bmecestado);
	    datos=(Button) findViewById(R.id.bmecdatos);
	    textEp =(TextView) findViewById(R.id.appmectext1);
	    textEc =(TextView) findViewById(R.id.appmectext2);
	    textEm =(TextView) findViewById(R.id.appmectext3);
	    textalt =(TextView) findViewById(R.id.mec_altura);
	    textmasa =(TextView) findViewById(R.id.mec_masa);
	    estado.setText("Iniciar");
		datos.setText("Datos");
		
		textEp.setText(mecview.getEnergiapotencialinicial());
		textEc.setText(mecview.getEnergiaCinetica());
		textEm.setText(mecview.getEnergiaMecanica());
		textalt.setText("Altura inicial :"+ mecview.altura);
		textmasa.setText("Masa  :"+ mecview.masainicial);
		

	}
	public boolean onCreateOptionsMenu(Menu menu) {
	    // Inflate the menu; this adds items to the action bar if it is present.
	    //getMenuInflater().inflate(R.menu.main, menu);
		getMenuInflater().inflate(R.menu.menu_mecanica, menu);
	    return true;
	}
	public void onBackPressed() {
		    MecView.alturaaux = MecView.altura;
			mecview.pausaAnima();
		    finish();
	
	}

public boolean onOptionsItemSelected(MenuItem item) { //acciones para los elementos del menu
	Intent i;

	AlertDialog.Builder alert = new AlertDialog.Builder(this);
    switch (item.getItemId()) {
    case R.id.mod_altura:
			
			alert.setTitle("Modificar Altura");
			alert.setMessage("Ingrese nueva altura");
			
			// Set an EditText view to get user input 
			final EditText input = new EditText(this);
			alert.setView(input);
			
			alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				  MecView.altura = Integer.parseInt(input.getText().toString());
				  MecView.alturaaux = Integer.parseInt(input.getText().toString());
				  bestado = 0;

					mecview.pausaAnima();
			  Intent intent = getIntent();
		         finish();
		         startActivity(intent);
		         
			  // Do something with value!
			  }
			});
			
			alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			  public void onClick(DialogInterface dialog, int whichButton) {
			    // Canceled.
			  }
			});
			
			alert.show();
        break;   
    case R.id.mod_masa:
	
	alert.setTitle("Modificar Masa");
	alert.setMessage("Ingrese nueva masa");
	
	// Set an EditText view to get user input 
	final EditText inputm = new EditText(this);
	alert.setView(inputm);
	
	alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
	public void onClick(DialogInterface dialog, int whichButton) {
		  MecView.masainicial = Integer.parseInt(inputm.getText().toString());
		   MecView.alturaaux = MecView.altura;
		  bestado = 0;

			mecview.pausaAnima();
	  Intent intent = getIntent();
         finish();
         startActivity(intent);
         
	  // Do something with value!
	  }
	});
	
	alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
	  public void onClick(DialogInterface dialog, int whichButton) {
	    // Canceled.
	  }
	});
	
	alert.show();
break; 
                
    }
    return true;
}
	
	public void estados (View view){
		if (bestado ==0 || bestado ==2){			
		estado.setText("Pausar");

		mecview.renuAnima();

		textEp.setText("Energia Pot:");
		textEc.setText("Energia Cin:");
		textEm.setText("Energia Mec:");
		
		bestado = 1;
		}
		else if (bestado == 1){
			estado.setText("Reanudar");
			/*Intent intent = getIntent();
	         finish();
	         startActivity(intent); */
			mecview.pausaAnima();
			textEp.setText(mecview.getEnergiapotencial());
			textEc.setText(mecview.getEnergiaCinetica());
			textEm.setText(mecview.getEnergiaMecanica());
			
			bestado=2;
		}
	}
	public void bdatos (View view){
		double []datos = mecview.getdatos();    	    
 	    Intent i = new Intent(this, ActDatosMecanica.class);
 	    Bundle bundles = new Bundle();
 		bundles.putDoubleArray("arraydatos", datos);
 	    i.putExtras(bundles);
 	    //startActivityForResult(i, REQUEST_CODE); // no recibire datos devuelta
 	    startActivity(i);
	}


}
